﻿using System;

namespace DCIMAP.GANS.GeneticAlgorithms.Legacy
{
    /// <summary>
    /// Evaluator for solving a SET-PARTITION problem.
    /// It works with BooleanArrayGenes gene type.
    /// </summary>
    [Serializable]
    class IQEvaluator : GeneEvaluator
    {
        /// <summary>
        /// Weights of items. Must be equal to length of genes
        /// </summary>
        private readonly int[] weights;

        /// <summary>
        /// Creates SetPartitionEvaluator class
        /// </summary>
        public IQEvaluator(int[] weights)
        {
            this.weights = weights;
        }

        /// <inheritdoc/>
        public override double Evaluate(Gene gene)
        {
            int S1 = 0;
            int S2 = 0;
            StringArrayGene arrayGene = (StringArrayGene)gene;
            for (int i = 0; i < weights.Length; i++)
                if (arrayGene.StringCode[i]=="1") S1 += weights[i];
                else S2 += weights[i];

            return 1.0 / (1 + Math.Abs(S1 - S2));
        }
    }
}